package cjlu.ea.h.controller;


import cjlu.ea.h.common.R;
import cjlu.ea.h.entity.ByszgshbLsb;
import cjlu.ea.h.entity.DBsyxwsqb;
import cjlu.ea.h.service.F101ByszgshbLsbService;
import cjlu.ea.h.service.F102DBsyxwsqbService;
import cjlu.ea.h.service.SUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 学位申请汇总表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-09-03
 */
@RestController
@CrossOrigin
@RequestMapping("/h/d-bsyxwsqb")
public class F102DBsyxwsqbController {
    @Autowired
    F102DBsyxwsqbService dBsyxwsqbService;
    @Autowired
    SUserService userService;

    //特殊获取毕业生数据
    @GetMapping("/gettsbyssj")
    public R getbyssj(){
        List<DBsyxwsqb> list = dBsyxwsqbService.list(null);
        System.out.println(list);
        return R.ok().data("list",list);
    }

    //分页条件查询
    @PostMapping("/getPagetsbysBytj/{current}/{limit}")
    public R getPagebyssjBytj(@PathVariable Long current, @PathVariable Long limit,
                              @RequestBody(required = false) DBsyxwsqb dBsyxwsqb, HttpServletRequest request){
        String roles = userService.getUserRoles(userService.getToken(request));
        List<Object> res;
        if(roles.equals("校级")){
            res = dBsyxwsqbService.getbyssj(dBsyxwsqb,current,limit,null);
        }else{
            res = dBsyxwsqbService.getbyssj(dBsyxwsqb,current,limit,roles);
        }

        return R.ok().data("list",(List<DBsyxwsqb>)res.get(0)).data("total",(Long)res.get(1));
    }


    @GetMapping("/getAllPagetsbyssjBytj/{current}/{limit}")
    public R getAllPagebyssjBytj(@PathVariable Long current,@PathVariable Long limit,HttpServletRequest request){
        String roles = userService.getUserRoles(userService.getToken(request));
        List<DBsyxwsqb> list;
        Page<DBsyxwsqb> page;
        //分页显示
        if(roles.equals("校级")){
            page = new Page<>(current,limit);
            dBsyxwsqbService.page(page,null);
        }else{
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("xy",roles);
            page = new Page<>(current,limit);
            dBsyxwsqbService.page(page,wrapper);
        }

        //数据查询获取
        list = page.getRecords();
        Long total = page.getTotal();

        return R.ok().data("list",list).data("total",total);
    }


    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@PathVariable Integer id){

        boolean result = dBsyxwsqbService.removeById(id);
        if (result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }

    }

    @GetMapping("gettsbysById/{id}")
    public R getbysById(@PathVariable Integer id){
        return R.ok().data("bys",dBsyxwsqbService.getById(id));
    }

    @PostMapping("update")
    public R updatebysById(@RequestBody DBsyxwsqb dBsyxwsqb){
        DBsyxwsqb newbys = new DBsyxwsqb();
        BeanUtils.copyProperties(dBsyxwsqb,newbys);
        List<DBsyxwsqb> list = new ArrayList<>();
        list.add(newbys);
        dBsyxwsqbService.saveOrUpdateBatch(list);
        return R.ok();
    }

    //新增按钮
    @PostMapping("saveOrUpdateOne")
    public R saveOrUpdateBysById(@RequestBody DBsyxwsqb dBsyxwsqb){
        DBsyxwsqb newbys = new DBsyxwsqb();
        BeanUtils.copyProperties(dBsyxwsqb,newbys);
        List<DBsyxwsqb> list = new ArrayList<>();
        list.add(newbys);
        dBsyxwsqbService.saveOrUpdateBatch(list);
        return R.ok();
    }




}

